package org.ikross.twitter.connector;

import java.io.InputStream;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.ikross.twitter.connector.parameter.IParameter;
import org.ikross.twitter.exception.ConnectorException;

public class ParametersRequiredConnector extends Connector {
	
	private IConnector task;
	private Set<String> requiredParameters;
	
	public ParametersRequiredConnector(IConnector task, Set<String> requiredParameters){
		if(task == null || requiredParameters == null)
			throw new IllegalArgumentException("Arguments must not be null");
		
		this.task=task;
		this.requiredParameters=requiredParameters;
	}

	@Override
	public void addParameter(String key, IParameter value) {
		this.task.addParameter(key, value);
	}
	
	@Override
	public Set<String> getKeyParameters() {
		return this.task.getKeyParameters();
	}

	@Override
	public IParameter getParameter(String key) {
		return this.task.getParameter(key);
	}

	@Override
	public boolean checkParameters() {
		if(this.task.checkParameters()){
			Iterator<String> keys = this.requiredParameters.iterator();
			while(keys.hasNext()){
				if(this.getParameter(keys.next())==null){
					return false;
				}
			}
			
			return true;
		}
		else
			return false;
	}

	@Override
	public InputStream execute() throws ConnectorException {
		return this.task.execute();
	}
	
	@Override
	public IConnector clone(){
		Iterator<String> keys = this.requiredParameters.iterator();
		Set<String> requiredParameters = new HashSet<String>();
		while(keys.hasNext()){
			requiredParameters.add(keys.next());
		}
		return new ParametersRequiredConnector(this.task.clone(), requiredParameters);
	}
}